<?php
namespace Application\controller;

use Cola\Controller;
use Cola\Request;

/**
 * 充值接口控制器
 */
class BasePaymentController extends Controller
{

    public function index(){

        // 为了优化支付回调，不要返回html，而返回json
        $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';

	    /* @var \Application\model\BasePayModel $m */
		$m = $this->model('Pay');
        // 验签
        if($m->signCheck()){
            $m->logfile('sign error');
            $this->error('sign error');
        }

		$msg = $m->initPayCallBack();
        if ($msg) $m->logfile($msg);

		$ret = is_timeout($m->time, 5, time());
		if($ret){
			//超时前后10分钟
			$m->logfile('timeout.....');
		}

        $ret = list($status) = $m->savePayment();
		if($status === '0'){
			$m->logfile('success');
			$m->__errmsg(SUCCESS);
		}else{
            $fail = isset($ret[1]) ? $ret[1] : 'pay fail';
			$m->logfile($fail);
			$m->__errmsg(PAY_ERR);
		}
	}

    public function __get($name = '')
    {
    	if(in_array($name, ['view','request','response','config'])){
    		return parent::__get($name);
    	} elseif ('Model' === substr($name, -5)){
    		return $this->$name = $this->model(substr($name, 0, -5));
    	}
    	// 从$_GET或$_POST中取值
    	return $this->$name = $this->get($name);
    }
}

